package App.controllertest;

import App.bean.Bean;
import App.bean.BeanDelete;
import App.bean.User;
import App.service.TestService;
import App.service.TestService1;
import App.vo.DemoVo;
import com.alibaba.fastjson.JSON;

import simple.form.cache.Cache;
import simple.form.controller.BaseController;
import simple.form.controller.Request;
import simple.form.controller.Response;
import simple.form.controller.SysContent;
import simple.form.controller.annotation.Controller;
import simple.form.controller.annotation.RequestMapping;

import simple.form.core.annotation.AutoWrite;
import simple.form.dao.MysqlCreator;
import simple.form.utils.QrcodeUtil;
import simple.form.utils.SafeConvertUtil;

import javax.imageio.ImageIO;
import java.io.IOException;

/**
 * Created by Administrator on 2017/2/23.
 */
@Controller
public class QrcodeController extends BaseController{

    @AutoWrite
    private TestService testService;

    @AutoWrite
    private TestService1 testService1;

    public TestService1 getTestService1() {
        return testService1;
    }

    public void setTestService1(TestService1 testService1) {
        this.testService1 = testService1;
    }

    public TestService getTestService() {
        return testService;
    }

    public void setTestService(TestService testService) {
        this.testService = testService;
    }



    @RequestMapping(url = "/test")
    public void test(Request request, Response response) throws IOException {
        //1,测试insert
//        System.out.println("insert sql  " + SysContent.sqlCreator.createInsert(new User()));
//        User user = new User();
//        user.setName("王五2");
//        user.setAddress("上海徐汇区");
//        user.setAge(23);
//        testService.doSave(user);
        //2,测试update
//        System.out.println("update sql  " + SysContent.sqlCreator.createUpdateByKey(new User()));
//        User user = new User();
//        user.setId(5);
//        user.setAddress("上海徐汇区");
//        user.setAge(12);
//        testService.update(user);
//        3,测试delete
//        System.out.println("delete sql  " + SysContent.sqlCreator.createDelete(new User()));
//        User user = new User();
//        user.setId(8);
//        testService.delete(user);
        //4,测试find
//        System.out.println("select sql  " + SysContent.sqlCreator.createFind(new User()));
//        User user = new User();
//        user.setId(5);
//        testService.doFind(user);
        //5,测试事务
        testService.testTransactionAdd();
        testService.testTransactionUpdate();

        //1,初始化请求参数bean并解析
        DemoVo demoVo = new DemoVo(request);
        pareBean(demoVo);
        //2,获取缓存
        Cache cache = getCache(demoVo);
        if(cache==null){
            if(demoVo.isValidBean()){
                //2.1,处理业务逻辑
                System.out.println("处理业务");
                //2.2,添加缓存
                cache = new Cache<String>("test");
                addCache(demoVo,cache);
                //3,返回
                response.response(cache.getCacheObject().toString());
            }else{
                //3,返回
                response.response("请求参数错误"+JSON.toJSONString(demoVo.getMessage()));
            }
        }else{
            //3,返回
            response.response(cache.getCacheObject().toString());
        }


    }

    @RequestMapping(url="/create_qrcode")
    public void create_qrcode(Request request, Response response) throws IOException {
        String content = SafeConvertUtil.toString(request.getRequest().getParameter("content"));
        response.getResponse().setContentType("image/jpeg");
        response.getResponse().setDateHeader("expries", -1);
        response.getResponse().setHeader("Cache-Control", "no-cache");
        response.getResponse().setHeader("Pragma", "no-cache");
        ImageIO.write(QrcodeUtil.create(content), "jpg", response.getResponse().getOutputStream());
    }
}
